//go:build latex

package canvas

import (
	"crypto/md5"
	"fmt"
	"os"
	"os/exec"
	"path"
	"testing"

	"github.com/tdewolff/test"
)

// from os/exec/exec_test.go
func helperCommand(command string, args ...string) *exec.Cmd {
	cs := []string{"-test.run=TestHelperProcess", "--", command}
	cs = append(cs, args...)
	cmd := exec.Command(os.Args[0], cs...)
	cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
	return cmd
}

func TestHelperProcess(*testing.T) {
	if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
		return
	}
	os.Exit(0)
}

func helperParseLaTeX(s string) (*Path, error) {
	if err := os.MkdirAll(tempDir, os.ModePerm); err != nil {
		panic(err)
	}

	hash := fmt.Sprintf("%x", md5.Sum([]byte(s)))
	w, err := os.Create(path.Join(tempDir, hash+".svg"))
	if err != nil {
		panic(err)
	}
	w.Write([]byte(s))
	w.Close()

	execCommand = helperCommand
	defer func() { execCommand = exec.Command }()
	return ParseLaTeX(s)
}

func TestLaTeX(t *testing.T) {
	// $x=\frac{5}{2}$
	_, err := helperParseLaTeX(`<?xml version='1.0' encoding='UTF-8'?>
	<!-- This file was generated by dvisvgm 2.6.3 -->
	<svg version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' width='24.144099pt' height='11.852377pt' viewBox='76.712329 54.347768 24.144099 11.852377'>
	<defs>
	<path id='g2-50' d='M3.521793 -1.26924H3.284682C3.263761 -1.115816 3.194022 -0.704359 3.103362 -0.63462C3.047572 -0.592777 2.510585 -0.592777 2.412951 -0.592777H1.129763C1.862017 -1.241345 2.106102 -1.436613 2.524533 -1.764384C3.040598 -2.175841 3.521793 -2.608219 3.521793 -3.270735C3.521793 -4.11457 2.782565 -4.630635 1.889913 -4.630635C1.025156 -4.630635 0.439352 -4.02391 0.439352 -3.382316C0.439352 -3.02665 0.739228 -2.991781 0.808966 -2.991781C0.976339 -2.991781 1.17858 -3.110336 1.17858 -3.361395C1.17858 -3.486924 1.129763 -3.731009 0.767123 -3.731009C0.983313 -4.226152 1.457534 -4.379577 1.785305 -4.379577C2.48269 -4.379577 2.84533 -3.835616 2.84533 -3.270735C2.84533 -2.66401 2.412951 -2.182814 2.189788 -1.931756L0.509091 -0.27198C0.439352 -0.209215 0.439352 -0.195268 0.439352 0H3.312578L3.521793 -1.26924Z'/>
	<path id='g2-53' d='M1.080946 -3.891407C1.436613 -3.800747 1.645828 -3.800747 1.75741 -3.800747C2.677958 -3.800747 3.221918 -4.428394 3.221918 -4.533001C3.221918 -4.609714 3.173101 -4.630635 3.138232 -4.630635C3.124284 -4.630635 3.110336 -4.630635 3.082441 -4.609714C2.915068 -4.546949 2.545455 -4.407472 2.02939 -4.407472C1.834122 -4.407472 1.464508 -4.42142 1.011208 -4.595766C0.941469 -4.630635 0.927522 -4.630635 0.920548 -4.630635C0.829888 -4.630635 0.829888 -4.553923 0.829888 -4.442341V-2.385056C0.829888 -2.266501 0.829888 -2.182814 0.941469 -2.182814C1.004234 -2.182814 1.011208 -2.196762 1.080946 -2.280448C1.380822 -2.66401 1.806227 -2.719801 2.050311 -2.719801C2.468742 -2.719801 2.657036 -2.385056 2.691905 -2.329265C2.817435 -2.099128 2.859278 -1.834122 2.859278 -1.429639C2.859278 -1.220423 2.859278 -0.808966 2.650062 -0.502117C2.475716 -0.251059 2.175841 -0.083686 1.834122 -0.083686C1.380822 -0.083686 0.913574 -0.334745 0.739228 -0.795019C1.004234 -0.774097 1.136737 -0.948443 1.136737 -1.136737C1.136737 -1.436613 0.878705 -1.492403 0.788045 -1.492403C0.774097 -1.492403 0.439352 -1.492403 0.439352 -1.115816C0.439352 -0.488169 1.011208 0.139477 1.84807 0.139477C2.740722 0.139477 3.521793 -0.523039 3.521793 -1.401743C3.521793 -2.189788 2.922042 -2.915068 2.057285 -2.915068C1.750436 -2.915068 1.387796 -2.838356 1.080946 -2.57335V-3.891407Z'/>
	<path id='g0-120' d='M3.327522 -3.008717C3.387298 -3.267746 3.616438 -4.184309 4.313823 -4.184309C4.363636 -4.184309 4.60274 -4.184309 4.811955 -4.054795C4.533001 -4.004981 4.333748 -3.755915 4.333748 -3.516812C4.333748 -3.35741 4.443337 -3.16812 4.712329 -3.16812C4.931507 -3.16812 5.250311 -3.347447 5.250311 -3.745953C5.250311 -4.26401 4.662516 -4.403487 4.323786 -4.403487C3.745953 -4.403487 3.39726 -3.875467 3.277709 -3.646326C3.028643 -4.303861 2.49066 -4.403487 2.201743 -4.403487C1.165629 -4.403487 0.597758 -3.118306 0.597758 -2.86924C0.597758 -2.769614 0.697385 -2.769614 0.71731 -2.769614C0.797011 -2.769614 0.826899 -2.789539 0.846824 -2.879203C1.185554 -3.935243 1.843088 -4.184309 2.181818 -4.184309C2.371108 -4.184309 2.719801 -4.094645 2.719801 -3.516812C2.719801 -3.20797 2.550436 -2.540473 2.181818 -1.145704C2.022416 -0.52802 1.673724 -0.109589 1.235367 -0.109589C1.175592 -0.109589 0.946451 -0.109589 0.737235 -0.239103C0.986301 -0.288917 1.205479 -0.498132 1.205479 -0.777086C1.205479 -1.046077 0.986301 -1.125778 0.836862 -1.125778C0.537983 -1.125778 0.288917 -0.86675 0.288917 -0.547945C0.288917 -0.089664 0.787049 0.109589 1.225405 0.109589C1.882939 0.109589 2.241594 -0.587796 2.271482 -0.647572C2.391034 -0.278954 2.749689 0.109589 3.347447 0.109589C4.373599 0.109589 4.941469 -1.175592 4.941469 -1.424658C4.941469 -1.524284 4.851806 -1.524284 4.821918 -1.524284C4.732254 -1.524284 4.712329 -1.484433 4.692403 -1.414695C4.363636 -0.348692 3.686177 -0.109589 3.367372 -0.109589C2.978829 -0.109589 2.819427 -0.428394 2.819427 -0.767123C2.819427 -0.986301 2.879203 -1.205479 2.988792 -1.643836L3.327522 -3.008717Z'/>
	<path id='g1-61' d='M6.844334 -3.257783C6.993773 -3.257783 7.183064 -3.257783 7.183064 -3.457036S6.993773 -3.656289 6.854296 -3.656289H0.886675C0.747198 -3.656289 0.557908 -3.656289 0.557908 -3.457036S0.747198 -3.257783 0.896638 -3.257783H6.844334ZM6.854296 -1.325031C6.993773 -1.325031 7.183064 -1.325031 7.183064 -1.524284S6.993773 -1.723537 6.844334 -1.723537H0.896638C0.747198 -1.723537 0.557908 -1.723537 0.557908 -1.524284S0.747198 -1.325031 0.886675 -1.325031H6.854296Z'/>
	</defs>
	<g id='page1'>
	<use x='76.712329' y='62.764633' xlink:href='#g0-120'/>
	<use x='85.173593' y='62.764633' xlink:href='#g1-61'/>
	<use x='96.885187' y='58.842026' xlink:href='#g2-53'/>
	<rect x='96.885187' y='60.074723' height='0.398484' width='3.971238'/>
	<use x='96.885187' y='66.200144' xlink:href='#g2-50'/>
	</g>
	</svg>`)
	test.Error(t, err)
}
